import Api from 'Utils/api';
import { message } from 'antd';
import { setRefreshInfo, requestLogout } from 'Actions/userAction';
import PERMISSION from '../constants/permission';

export const checkRedirectPermission = function (next, replace, callback) {
  if (!window.store.getState().userReducer.isLogin) {
    replace('/login');
    callback();
    return;
  }
  if (next.location.pathname === '/system/visual/shian') {
    let pathname = '';
    const userPermissions = window.store.getState().userReducer.permissions;
    if (PERMISSION.isPermissionAllow([PERMISSION.VISUAL_DISPLAY], userPermissions)) {
      // 可视化展示系统
      pathname = '/system/visual/shian';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.COMMAND_MANAGEMENT], userPermissions)) {
      // 指挥调度系统
      pathname = '/system/dispatch/dispatch';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.DECISION_SUPPORT], userPermissions)) {
      // 决策支持系统
      pathname = '/system/dispatch/support';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.FOOD_SAFETY_MAP], userPermissions)) {
      // 大数据分析系统
      pathname = '/system/bigdata/map';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.EARLY_WARNING_SYSTEM], userPermissions)) {
      // 预警体系
      pathname = '/system/alert/main';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.SOCIAL_GOVERNANCE], userPermissions)) {
      // 社会共治管理
      pathname = '/system/corule/exhibition';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.THIRD_DATA_MANAGEMENT], userPermissions)) {
      // 数据导入
      pathname = '/system/data/mobile_law';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.ROLE_MANAGE], userPermissions)) {
      // 系统管理
      pathname = '/system/manager/role';
    } else if (PERMISSION.isPermissionAllow([PERMISSION.SUPER_MANAGE], userPermissions)) {
      // 系统管理
      pathname = '/system/manager/account';
    }
    if (pathname !== next.location.pathname) {
      replace(pathname);
    }
  }
  callback();
};

export const checkPermission = function (next, replace, callback) {
  if (!window.store.getState().userReducer.isLogin) {
    replace('/login');
    callback();
    return;
  }
  if (next.location.pathname === '/system/dispatch/dispatch') {
    const api = new Api({
      method: 'GET',
      path: '/tasks/judge_state',
      success: (res) => {
        if (res === -1) {
          callback();
        } else {
          replace(`/system/dispatch/dispatch/task/${res}`);
          callback();
        }
      },
      error: (err) => {
        message.error(err);
      }
    });
    api.run();
  }
  callback();
};

export const validate = function (next, replace, callback) {
  const store = window.store;
  if (window.store.getState().userReducer.isLogin) {
    const api = new Api({
      method: 'GET',
      path: '/own',
      success: (res) => {
        store.dispatch(setRefreshInfo(res));
        callback();
      },
      error: (err) => {
        message.error(err);
        store.dispatch(requestLogout());
        replace('/login');
        callback();
      }
    });
    api.run();
  }
  callback();
};

